package com.huawei.esdk.tp.device.aa;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.Base64Utils;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.common.TPAAUtils;
import com.huawei.esdk.tp.cxf.enumgen.ClientType;
import com.huawei.esdk.tp.southcommu.cxf.gen.AuthenticateResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.KeepAliveResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.KeepAliveSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.LoginRequestResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.LoginRequestSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.LogoutResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.LogoutSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SmcErr2South;
import com.huawei.esdk.tp.southcommu.cxf.gen.SmcExternal2SoapSouth;

public class SMCAAService
{
    private static final Logger LOGGER = Logger.getLogger(SMCAAService.class);
    
    private ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter;
    
    public SMCAAService(ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter)
    {
        this.cxfSOAPProtocolAdapter = cxfSOAPProtocolAdapter;
    }
    
    public LoginRequestResponseSouth smcLoginRequest(String userName)
    {
        LoginRequestSouth reqMessage = new LoginRequestSouth();
        reqMessage.setClientType(ClientType.API);
        reqMessage.setUserName(userName);
        reqMessage.setVersion(2);
        LoginRequestResponseSouth res = null;
        
        try
        {
            res = sendSoapMessage(LoginRequestResponseSouth.class, reqMessage, "loginRequest");
        }
        catch (InstantiationException e)
        {
            res = null;
            LOGGER.error("", e);
        }
        catch (IllegalAccessException e)
        {
            res = null;
            LOGGER.error("", e);
        }
        catch (ProtocolAdapterException e)
        {
            //res = null;DTS2015011901913 修改
            res = new LoginRequestResponseSouth();
            SmcErr2South err2South = new SmcErr2South();
            err2South.setErrCode(e.getErrorCode());
            res.setLoginRequestResult(err2South);
            LOGGER.error("", e);
        }
        return res;
    }
    
    public AuthenticateResponseSouth smcAuthorize(byte[] randomSequence, String password)
    {
        String encrptPassword = "";
        try
        {
            encrptPassword = TPAAUtils.getEncryptPassword(password, Base64Utils.encode(randomSequence));
        }
        catch (SDKException e)
        {
            LOGGER.error("", e);
            return buildRes(ErrInfo.SDK_SYSTEM_ERRORCODE, "SMC Authenticate error!");
        }
        
        return smcAuthorize(encrptPassword);
    }
    
    public AuthenticateResponseSouth smcAuthorize(String encrptPassword)
    {
        int errcode = 0;
        String errDescription = null;
        
        AuthenticateResponseSouth res = null;
        
        com.huawei.esdk.tp.southcommu.cxf.gen.AuthenticateSouth reqMessage =
            new com.huawei.esdk.tp.southcommu.cxf.gen.AuthenticateSouth();
        try
        {
            reqMessage.setEncryptPassword(Base64Utils.getFromBASE64(encrptPassword));
            res = sendSoapMessage(AuthenticateResponseSouth.class, reqMessage, "authenticate");
        }
        catch (InstantiationException e)
        {
            errcode = ErrInfo.SDK_SYSTEM_ERRORCODE;
            errDescription = "SMC Authenticate error!";
            LOGGER.error("", e);
        }
        catch (IllegalAccessException e)
        {
            errcode = ErrInfo.SDK_SYSTEM_ERRORCODE;
            errDescription = "SMC Authenticate error!";
            LOGGER.error("", e);
        }
        catch (ProtocolAdapterException e)
        {
            errcode = ErrInfo.SDK_SYSTEM_ERRORCODE;
            errDescription = "SMC Authenticate error!";
            LOGGER.error("", e);
        }
        catch (Exception e)
        {
            errcode = ErrInfo.SDK_SYSTEM_ERRORCODE;
            errDescription = "SMC Authenticate error!";
            LOGGER.error("", e);
        }
        
        if (res == null)
        {
            res = buildRes(errcode, errDescription);
        }
        
        return res;
    }
    
    private AuthenticateResponseSouth buildRes(long errCode, String errDescription)
    {
        int subSystem = 1;
        AuthenticateResponseSouth res = new AuthenticateResponseSouth();
        SmcErr2South err = new SmcErr2South();
        err.setErrCode(errCode);
        err.setSubSystem(subSystem);
        err.setDescription(errDescription);
        res.setAuthenticateResult(err);
        return res;
    }
    
    public LogoutResponseSouth smcLogout()
    {
        LogoutSouth logout = new LogoutSouth();
        
        LogoutResponseSouth res = null;
        try
        {
            res = sendSoapMessage(LogoutResponseSouth.class, logout, "logout");
        }
        catch (InstantiationException e)
        {
            LOGGER.error("", e);
        }
        catch (IllegalAccessException e)
        {
            LOGGER.error("", e);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("", e);
        }
        
        if (res == null)
        {
            res = new LogoutResponseSouth();
            SmcErr2South err = new SmcErr2South();
            err.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            err.setSubSystem(1);
            res.setLogoutResult(err);
        }
        return res;
    }
    
    public KeepAliveResponseSouth smcKeepAlive()
    {
        KeepAliveSouth keepAlive = new KeepAliveSouth();
        
        KeepAliveResponseSouth res = null;
        try
        {
            res = sendSoapMessage(KeepAliveResponseSouth.class, keepAlive, "keepAlive");
        }
        catch (InstantiationException e)
        {
            LOGGER.error("keepAlive() exception", e);
        }
        catch (IllegalAccessException e)
        {
            LOGGER.error("keepAlive() exception", e);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("keepAlive() exception", e);
        }
        
        if (res == null)
        {
            res = new KeepAliveResponseSouth();
            SmcErr2South err = new SmcErr2South();
            err.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            err.setSubSystem(1);
            res.setKeepAliveResult(err);
        }
        
        return res;
    }
    
    @SuppressWarnings("unchecked")
    public <T1> T1 sendSoapMessage(Class<T1> T1Cls, Object requestMessage, String srcMethod)
        throws ProtocolAdapterException, InstantiationException, IllegalAccessException
    {
        // send message to SMC
        T1 res =
            (T1)cxfSOAPProtocolAdapter.syncSendMessageWithCxf(requestMessage,
                SmcExternal2SoapSouth.class.getName(),
                srcMethod);
        
        return res;
    }
}
